<?php

class Controller_Certified extends Controller_Website {

    public function action_personal() {
        
        $m_user = Model::factory('user');
        $where = array();
        $where['ORDER'] = 'create_time DESC'; //排序方式
        $where['type'] = 2; //用户类型  2为企业用户
        $list = $m_user->getAll($where)->as_array();
//         var_dump($list);
        $m_CertifiedSubmit = Model::factory('CertifiedSubmit');
        $where2 = array(
            'type' => 1  //认证类型  1为个人认证
        );
        $total = $m_CertifiedSubmit->count($where2);
        $pager = new Pager($total, 20);
        $CertifiedSubmit = $m_CertifiedSubmit->select($pager->offset, $pager->size, $where2)->as_array();
//         var_dump($CertifiedSubmit);
        //用户名
        foreach ($CertifiedSubmit as &$item) {
            foreach ($list as $item2) {
                if ($item2['id']==$item['user_id']) {
                    $item['username'] = $item2['username'];
                }
            }
        }
        //用户创建时间
        foreach ($CertifiedSubmit as &$item) {
            foreach ($list as $item2) {
                if ($item2['id']==$item['user_id']) {
                    $item['create_time'] = $item2['create_time'];
                }
            }
        }
//                 var_dump($CertifiedSubmit);exit;

        $this->content = View::factory('certified/personal');
        $this->content->CertifiedSubmit = $CertifiedSubmit;
        $this->content->pager = $pager;
        
    }
    
    public function action_company() {
        $m_user = Model::factory('user');
        $where = array();
        $where['ORDER'] = 'create_time DESC'; //排序方式
        $where['type'] = 2; //用户类型  2为企业用户
        $list = $m_user->getAll($where)->as_array();
        
        $m_CertifiedSubmit = Model::factory('CertifiedSubmit');
        $where2 = array(
            'type' => 2  //认证类型  2为企业认证
        );
        $total = $m_CertifiedSubmit->count($where2);
        $pager = new Pager($total, 20);
        $CertifiedSubmit = $m_CertifiedSubmit->select($pager->offset, $pager->size, $where2)->as_array();
        
        //用户名
        foreach ($CertifiedSubmit as &$item) {
            foreach ($list as $item2) {
                if ($item['user_id'] == $item2['id']) {
                    $item['username'] = $item2['username'];
                }
            }
        }
        //用户创建时间
        foreach ($CertifiedSubmit as &$item) {
            foreach ($list as $item2) {
                if ($item2['id']==$item['user_id']) {
                    $item['create_time'] = $item2['create_time'];
                }
            }
        }
//                 var_dump($CertifiedSubmit);exit;

        $this->content = View::factory('certified/company');
        $this->content->CertifiedSubmit = $CertifiedSubmit;
        $this->content->pager = $pager;
    
    }
    
    public function action_close() {
        $id = $_GET['id'];
        $data = array(
            'status' => '2',//改成未认证 (认证不通过)
            'review_time' => time()
        );
        $m_CertifiedSubmit = Model::factory('CertifiedSubmit');
        $ret = $m_CertifiedSubmit->update($data, array('id'=>$id));
        
        $CertifiedSubmit = $m_CertifiedSubmit->getRowById($id);
        $m_personInfo = Model::factory('personInfo');
        $data2 = array(
            'is_certified'=>$CertifiedSubmit['status'],
            
        );
        $personInfo = $m_personInfo->update($data2, array('uid'=>$CertifiedSubmit['user_id']));
        
        if ($ret !== false && $personInfo !== false && $CertifiedSubmit['type'] == 1) {
            $this->redirect('certified/personal');
        }
        if ($ret !== false && $personInfo !== false && $CertifiedSubmit['type'] == 2) {
            $this->redirect('certified/company');
        }
    }
    
    public function action_open() {
        $id = $_GET['id'];
        $data = array(
            'status' => '1',//改成已认证(认证通过)
            'review_time' => time()
        );
        $m_CertifiedSubmit = Model::factory('CertifiedSubmit');
        $ret = $m_CertifiedSubmit->update($data, array('id'=>$id));
        
        
        $CertifiedSubmit = $m_CertifiedSubmit->getRowById($id);
        $m_personInfo = Model::factory('personInfo');
        $data2 = array(
            'nick_name'=>$CertifiedSubmit['nick_name'],
            'real_name'=>$CertifiedSubmit['real_name'],
            'pic'=>$CertifiedSubmit['head_pic'],
            'sfz_pic1'=>$CertifiedSubmit['sfz_pic1'],
            'sfz_pic2'=>$CertifiedSubmit['sfz_pic2'],
            'recommended_code'=>$CertifiedSubmit['recommended_code'],
            'province'=>$CertifiedSubmit['province'],
            'city'=>$CertifiedSubmit['city'],
            'area'=>$CertifiedSubmit['area'],
            'company_name'=>$CertifiedSubmit['company_name'],
            'legal_person_name'=>$CertifiedSubmit['legal_person_name'],
            'yyzz_pic1'=>$CertifiedSubmit['yyzz_pic1'],
            'yyzz_pic2'=>$CertifiedSubmit['yyzz_pic2'],
            'certified_type'=>$CertifiedSubmit['type'],
            'is_certified'=>$CertifiedSubmit['status'],
            'company_tel'=>$CertifiedSubmit['company_tel'],
            'company_email'=>$CertifiedSubmit['company_email'],
            'phone'=>$CertifiedSubmit['phone'],
            'id_card'=>$CertifiedSubmit['ID_card'],
            'yyzz_code'=>$CertifiedSubmit['yyzz_code'],
            'user_type'=>2
        );
        
        $personInfo = $m_personInfo->update($data2, array('uid'=>$CertifiedSubmit['user_id']));
        
//         var_dump($CertifiedSubmit['user_id']);exit;
        if ($ret !== false && $personInfo !== false && $CertifiedSubmit['type'] == 1) {
            $this->redirect('certified/personal');
        }
        
        if ($ret !== false && $personInfo !== false && $CertifiedSubmit['type'] == 2) {
            $this->redirect('certified/company');
        }
    }
    
    //查看认证信息 个人
    public function action_personInfo() {
        $id = $_GET['id'];
        $m_CertifiedSubmit = Model::factory('CertifiedSubmit');
        $CertifiedSubmit = $m_CertifiedSubmit->getRowById($id);
        
        $this->content = View::factory('certified/personInfo');
        $this->content->CertifiedSubmit = $CertifiedSubmit;
    }
    
    //查看认证信息 企业
    public function action_companyInfo() {
        $id = $_GET['id'];
        $m_CertifiedSubmit = Model::factory('CertifiedSubmit');
        $CertifiedSubmit = $m_CertifiedSubmit->getRowById($id);
    
        $this->content = View::factory('certified/companyInfo');
        $this->content->CertifiedSubmit = $CertifiedSubmit;
    }
}

